package com.example.moshudriver.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.BeeFramework.model.BusinessResponse;
import com.BeeFramework.view.ToastView;
import com.sgr.dykdriver.R;
import com.example.moshudriver.model.UserModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.SIMPLE_USER;
import com.example.moshudriver.protocol.userchange_passwordResponse;
import com.example.moshudriver.tools.APIErrorCode;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.example.moshudriver.utils.MyApplication;
import com.external.androidquery.callback.AjaxStatus;

//重置密码
public class B0_reset_password extends Activity implements OnClickListener,
		BusinessResponse {
	public static String MOBILE = "mobile";
	private EditText mNewPassword;
	private EditText mReNewPassword;
	private Button mSave;
	private UserModel mUserModel;
	private TextView mTitle;
	private ImageView mBack;
	private String mobile;
//	private SIMPLE_USER user;;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.b0_reset_password);
		mTitle = (TextView) findViewById(R.id.top_view_title);
		mTitle.setText(getString(R.string.change_login_password));
		mBack = (ImageView) findViewById(R.id.top_view_back);
		mNewPassword = (EditText) findViewById(R.id.b0_et_new_password);
		mReNewPassword = (EditText) findViewById(R.id.b0_et_reset_password);
		mSave = (Button) findViewById(R.id.b0_btn_save);
		mSave.setOnClickListener(this);
		mBack.setOnClickListener(this);
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		mobile = bundle.getString(B0_reset_password.MOBILE);

		mUserModel = new UserModel(this);
		mUserModel.addResponseListener(this);
		MyApplication.getInstance().addActivity(this);
	}

	@Override
	public void onClick(View v) {

		String new_password = mNewPassword.getText().toString().trim();
		String re_new_password = mReNewPassword.getText().toString().trim();
		switch (v.getId()) {
		case R.id.top_view_back:
			finish();
			break;
		case R.id.b0_btn_save:
			System.out.println("进行修改密码");
			if ("".equals(new_password)) {
				ToastView toast = new ToastView(B0_reset_password.this,
						getString(R.string.input_new_password));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mNewPassword.requestFocus();
				mNewPassword.setText("");
			} else if (new_password.length() < 6 || new_password.length() > 20) {
				ToastView toast = new ToastView(B0_reset_password.this,
						getString(R.string.new_password_wrong_format_hint));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mNewPassword.requestFocus();
			} else if (!new_password.equals(re_new_password)) {
				ToastView toast = new ToastView(B0_reset_password.this,
						getString(R.string.two_passwords_differ_hint));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mReNewPassword.requestFocus();
			} else {
				SharedPreferences sharedPreferences = getSharedPreferences(O2OMobileAppConst.USERINFO, MODE_PRIVATE);
				int role = sharedPreferences.getInt("role", 0);
				if (role==3) {
					mUserModel.copartnerfindPassWord(mobile, new_password);
				} else {
					mUserModel.findPassWord(mobile, new_password);
				}
			}
			break;

		}

	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url.endsWith(ApiInterface.USER_FIND_PWD)) {
			userchange_passwordResponse response = new userchange_passwordResponse();
			response.fromJson(jo);
			System.out.println("test jo:" + jo);
			if (response.succeed == 1) {
				ToastView toast = new ToastView(B0_reset_password.this,
						getString(R.string.change_password_success));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				Intent intent = new Intent(B0_reset_password.this,
						B0_SigninActivity.class);
				startActivity(intent);
				finish();
			}
			if (url.endsWith(ApiInterface.USER_CHANGE_PASSWORD)) {
				// userchange_passwordResponse response = new
				// userchange_passwordResponse();
				// response.fromJson(jo);
				// if (response.succeed == 1) {
				// ToastView toast = new ToastView(B0_reset_password.this,
				// getString(R.string.change_password_success));
				// toast.setGravity(Gravity.CENTER, 0, 0);
				// toast.show();
				// Intent intent = new Intent(B0_reset_password.this,
				// B0_SigninActivity.class);
				// startActivity(intent);
				// finish();
			} else {
				if (response.error_code == APIErrorCode.ERROR_PASSWORD) {

				}
			}

		}

	}

}
